package com.kungfuhacking.wristbandpro.location.presenter;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.location.bean.AreaListEntity;
import com.kungfuhacking.wristbandpro.location.bean.SafeAreaBean;
import com.kungfuhacking.wristbandpro.location.view.ISafeAreaListView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.utils.LogUtil;

import java.util.List;

import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 安全区域列表
 * Created by Aaron on 17/5/5.
 */

public class ISafeAreaPresenter implements BasePresenter {

    ISafeAreaListView iSafeAreaView;
    private List<AreaListEntity> areaLists;

    private String TAG = getClass().getSimpleName();

    public ISafeAreaPresenter(ISafeAreaListView iSafeAreaView) {
        this.iSafeAreaView = iSafeAreaView;
    }

    /**
     * 获取安全区域列表
     */
    public void getSafeArea() {

        JSONObject j = new JSONObject();
        j.put("token", iSafeAreaView.getToken());
        j.put("id", iSafeAreaView.getSp().getString(Constant.CURRENTOLDID, ""));

        RetrofitHttpUtil.getInstance().getAreaList(
                new Observer<SafeAreaBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                        iSafeAreaView.showLoading("获取中...");
                    }

                    @Override
                    public void onNext(SafeAreaBean value) {

                        if (value.getCode() == 0) {

                            iSafeAreaView.hideLoading();
                            setSafeListData(value);
                        } else if (value.getCode() == Constant.ERR0R_RELOGIN) {

                            iSafeAreaView.nextActivityWithSingleTask(LoginActivity.class);
                        } else {
                            iSafeAreaView.hideLoading();
                            iSafeAreaView.showErr(value.getCode_msg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        iSafeAreaView.hideLoading();
                        iSafeAreaView.showErr("网络错误 : " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                }
                , new RequestBean().setData(j.toString()));

    }

    private void setSafeListData(SafeAreaBean value) {
        if (value.getCode() == 0) {
            areaLists = value.getAreaList();
            iSafeAreaView.setAreaData(value.getAreaList());
        } else
            iSafeAreaView.showErr(value.getCode_msg());
    }

    /**
     * 删除安全区域
     *
     * @param itemId
     */
    public void deleteSafeArea(String itemId) {

        JSONObject j = new JSONObject();
        j.put("token", iSafeAreaView.getToken());
        j.put("id", iSafeAreaView.getSp().getString(Constant.CURRENTOLDID, ""));
        j.put("areaId", itemId);
        RetrofitHttpUtil.getInstance().getService().delArea(new RequestBean().setData(j.toString())).flatMap(new Function<Basebean, ObservableSource<SafeAreaBean>>() {
            @Override
            public ObservableSource<SafeAreaBean> apply(Basebean basebean) throws Exception {

                Log.d(TAG, " 删除 返回的 数据 :" + basebean.getCode_msg());

                iSafeAreaView.showErr(basebean.getCode() == 0 ? "删除成功" : basebean.getCode_msg());

                JSONObject json = new JSONObject();
                json.put("token", iSafeAreaView.getToken());
                json.put("id", iSafeAreaView.getSp().getString(Constant.CURRENTOLDID, ""));
                return RetrofitHttpUtil.getInstance().getService().getAreaList(new RequestBean().setData(json.toString()));

            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<SafeAreaBean>() {
                    @Override
                    public void accept(SafeAreaBean safeAreaBean) throws Exception {
                        if (safeAreaBean != null)
                            setSafeListData(safeAreaBean);
                        else
                            LogUtil.d(TAG, "删除错误??? ");
                    }
                });

    }


    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
